<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/7/19
 * Time: 20:08
 */

namespace App\Http\Controllers\Home;


use App\Http\Controllers\Controller;
use App\Models\CompanyApply;
use Illuminate\Http\Request;

class CompanyController extends Controller
{
    public function __construct()
    {

    }

    /**
     * 企业进驻首页
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index()
    {
        return view('home.index');
    }

    /**
     * 企业申请页面
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function apply()
    {
        return view('home.apply');
    }

    /**
     * 上传图片
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse\
     */
    public function uploadImages(Request $request)
    {
        $file = $request->file('file');
        $fileName = $file->getClientOriginalName();
        $relatedPath = 'company/' . date('Y-m-d-His');
        $path = $file->store($relatedPath);
        if ($path) {
            $path = env('STATIC_URL') . $path;
            $data['path'] = $path;
            $data['file_name'] = $fileName;
            return successJson($data);
        } else {
            return errorJson();
        }
    }

    public function submit(Request $request)
    {
        // 表单验证
        $validator = \Validator::make($request->all(), [
            'company_name' => 'required',
            'contact_man' => 'required',
            'contact_way' => 'required',
            'release_type' => 'required',
            'target_throng' => 'required'
        ]);
        // 表单验证失败处理
        if ($validator->fails()) {
            return errorJson('','表单数据异常，提交失败');
        }
        $data = $inputs = $request->input();
        unset($data['image']);
        // 检查是否已经提交过
        $isHasCompany = CompanyApply::where('company_name',$data['company_name'])->first();
        if (!empty($isHasCompany)) {
            return errorJson('','贵公司已经提交过资料了，无须重复提交申请');
        }
        $res = CompanyApply::create($data);
        if ($res) {
            return successJson('','提交申请成功，请耐心等候专人联系');
        } else {
            return errorJson('','提交失败');
        }
    }
}